﻿using Microsoft.AspNetCore.Mvc;
using PHMEE.Admin.Controllers;
using PHMEE.ToolKits;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using static PHMEE.PHMEEAdminPermissions;

namespace PHMEE.Admin.HttpApi.Controllers.DaqInfo.TdDevices
{
    /// <summary>
    /// 创建TD数据结构
    /// </summary>
    [Route("phmeeadmin/api/[controller]/[action]")]
    public class TdDeviceController : PHMEEAdminController
    {
        private readonly ITdDeviceAppService _deviceAppService;

        public TdDeviceController(ITdDeviceAppService tdDeviceAppService)
        {
            _deviceAppService = tdDeviceAppService;
        }
        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> CreateDataBase(CreateDatabaseDto input)
        {
            try
            {
                var guid = await _deviceAppService.CreateDataBaseAsync(input);
                return Ok(new { message = "创建数据库成功", data = guid });
            }
            catch (UserFriendlyException ex)
            {
                return BadRequest(new { message = ex.Message, details = ex.Data });
            }
        }
        /// <summary>
        /// 判断数据库是否存在
        /// </summary>
        /// <param name="dbName"></param>
        /// <returns>如果返回true表示已经存在，false表示没存在</returns>
        [HttpGet]
        public async Task<bool> GetDbName(string dbName)
        {
            return await _deviceAppService.GetDbNameAsync(dbName);
        }
        /// <summary>
        /// 获取所有数据库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<DatabaseDto>> GetAllDatabases()
        {
            return await _deviceAppService.GetAllDatabasesAsync();
        }
        /// <summary>
        /// 删除数据库(会删除对应所有超级表)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteDataBase(Guid id)
        {
            await _deviceAppService.DeleteDataBaseAsync(id);
        }
        /// <summary>
        /// 创建超级表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> CreateSuperTable(CreateSuperTableDto input)
        {
            return await _deviceAppService.CreateSuperTableAsync(input);
        }
        /// <summary>
        /// 根据数据库ID获取超级表
        /// </summary>
        /// <param name="databaseId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SuperTableDto>> GetSuperTablesByDatabaseId(Guid databaseId)
        {
            return await _deviceAppService.GetSuperTablesByDatabaseIdAsync(databaseId);
        }
        /// <summary>
        /// 删除超级表（会删除对应所有列和子表）
        /// </summary>
        /// <param name="superTableId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteSuperTable(Guid superTableId)
        {
            await _deviceAppService.DeleteSuperTableAsync(superTableId);
        }
        /// <summary>
        /// 添加超级表属性
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task AddAttributeToSuperTable(CreateAndUpdateAttributesDto input)
        {
            await _deviceAppService.CreateUpdateAttributesForSuperTableAsync(input);
        }
        /// <summary>
        /// 批量创建超级表的属性或者更新属性
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task CreateUpdateAttributesForSuperTable(CreateAndUpdateAttributesDto input)
        {
            await _deviceAppService.CreateUpdateAttributesForSuperTableAsync(input);
        }
        /// <summary>
        /// 根据超级表ID获取所有子表
        /// </summary>
        /// <param name="superTableId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SubTableDto>> GetSubTablesBySuperTableId(Guid superTableId)
        {
            return await _deviceAppService.GetSubTablesBySuperTableIdAsync(superTableId);
        }
        /// <summary>
        /// 根据子表实例id查询子表数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SubTableDto>> GetSubTablesAsync(List<Guid> ids)
        {
            return await _deviceAppService.GetSubTablesAsync(ids);
        }
        /// <summary>
        /// 删除单个属性
        /// </summary>
        /// <param name="attributeId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteAttribute(Guid attributeId)
        {
            await _deviceAppService.DeleteAttributeAsync(attributeId);
        }
        /// <summary>
        /// 创建子表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> CreateSubTable(CreateSubTableDto input)
        {
            return await _deviceAppService.CreateSubTableAsync(input);
        }
        /// <summary>
        /// 删除单个子表
        /// </summary>
        /// <param name="subTableId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteSubTable(Guid subTableId)
        {
            await _deviceAppService.DeleteSubTableAsync(subTableId);
        }
        /// <summary>
        /// 根据超级表或子表ID获取属性
        /// </summary>
        /// <param name="tableId">超级或者子表的id</param>
        /// <param name="isSuperTable">是超级表传入ture，子表传入false</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<AttributeDto>> GetAttributesByTableId(Guid tableId, bool isSuperTable)
        {
            return await _deviceAppService.GetAttributesByTableIdAsync(tableId, isSuperTable);
        }
        /// <summary>
        /// 批量创建或更新测点与子表实例关系
        /// </summary>
        /// <param name="relations"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> CreateOrUpdatePointRelation(List<PointRelationDto> relations)
        {
            try
            {
                await _deviceAppService.CreateOrUpdatePointRelationAsync(relations);
                return Ok(new { message = "测点与子表的关系已成功更新" });
            }
            catch (UserFriendlyException ex)
            {
                return BadRequest(new { message = ex.Message, details = ex.Data });
            }
        }
        /// <summary>
        /// 根据子表实例 ID 获取所有测点关系
        /// </summary>
        /// <param name="subTableId">子表id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PointRelationDto>> GetRelationsByInstanceId(Guid subTableId)
        {
            return await _deviceAppService.GetRelationsByInstanceIdAsync(subTableId);
        }
        /// <summary>
        /// 获取测点与子表实例的关联关系集合。
        /// </summary>
        /// <param name="pointIds">测点集合</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PointRelationDto>> GetRelations(List<Guid> pointIds)
        {
            return await _deviceAppService.GetRelationsAsync(pointIds);
        }
        /// <summary>
        /// 删除测点与子表实例的关系(可批量删除)--测点删除时执行
        /// </summary>
        /// <param name="pointIds">测点id集合</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteRelations(List<Guid> pointIds)
        {
            await _deviceAppService.DeleteRelationsAsync(pointIds);
        }

        /// <summary>
        /// Mysql种生成TD的种子数据
        /// </summary>
        /// <param name="dbPrefix">数据库类型（1:能效，2:Ams，3:振动）</param>
        /// <param name="vesselId">船舶ID</param>
        /// <param name="dbDescription">数据库描述</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> CreateTdDataSeed(DbPrefixEnum dbPrefix, Guid vesselId, string dbDescription)
        {
            bool result = await _deviceAppService.TdDataSeedAsync(dbPrefix, vesselId, dbDescription);
            if (result)
                return Ok(new { message = "数据库种子数据创建成功" });
            else
                return BadRequest(new { message = "数据库种子数据创建失败" });
        }

        /// <summary>
        /// 指定数据库名称生成TD数据库脚本
        /// </summary>
        /// <param name="dbName">数据库名称</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GenerateSqlScripts(string dbName)
        {
            try
            {
                await _deviceAppService.GenerateSqlScriptsAsync(dbName);
                return Ok(new { message = "success" });
            }
            catch (UserFriendlyException ex)
            {
                return BadRequest(new { message = ex.Message, details = ex.Data });
            }
        }

        /// <summary>
        /// 获取TdEngine属性字段列类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<OperationOptionInt> GetTdEngineAttributeDataTypes()
        {
            return _deviceAppService.GetTdEngineAttributeDataTypes();
        }
    }
}
